﻿$(document).ready(function () {

    // Constructors
    $("#btnSticky").click(function () {

        $("#hdfStickyID").val(null);

        $(".txtStickyDate").removeAttr('disabled');

        $("#sticky-dialog").show();
        $("#sticky-dialog").dialog("open");
    });

    $("#sticky-dialog").dialog({
        autoOpen: false,
        width: 639,
        modal: true,
        buttons: {
            OK: function () {
                var url = '';

                if ($("#hdfStickyID").val() == '') {
                    // create
                    url = "/Update.asmx/CreateSticky";
                }
                else {
                    // update
                    url = "/Update.asmx/UpdateSticky";
                }
                var params = "{ date: '" + $(".txtStickyDate").val() + "', description: '" + $(".txtDescription").val() + "' }";

                $.ajax({
                    url: url,
                    data: params,
                    dataType: "json",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        result = data.d;
                        if (result)
                            window.location.reload();
                        else
                            alert('This sticky is exists. Please remove it before create new sticky!');
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert('Update failed.');
                    }
                });
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        },
        close: function () {
        }
    });

    $(function () {
        // Initialize
        $('#set1 *').tooltip();

        // Datepicker
        $(".datepicker").datepicker({
            constrainInput: true,
            onSelect: function () { }
        });
    });
});

function createSticky(date) {

    $("#hdfStickyID").val(null);
    $(".txtStickyDate").val(date);

    $(".txtStickyDate").removeAttr('disabled');

    $("#sticky-dialog").dialog("open");
}

function editSticky(date) {
    $("#hdfStickyID").val(true);
    $(".txtStickyDate").val(date);

    var params = "{ date: '" + $(".txtStickyDate").val() + "' }";

    $.ajax({
        url: "/Get.asmx/GetSticky",
        data: params,
        dataType: "json",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            result = data.d;
            $(".txtDescription").val(result);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert('Get sticky failed.');
        }
    });

    $(".txtStickyDate").attr('disabled', true);

    $("#sticky-dialog").dialog("open");
}

function deleteSticky(date) {

    if (confirm('Are you sure you want to remove this sticky?')) {// Remove the sticky
        var params = "{ date: '" + date + "' }";

        $.ajax({
            url: "/Update.asmx/DeleteSticky",
            data: params,
            dataType: "json",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                result = data.d;
                if (result)
                    window.location.reload();
                else
                    alert("Can't delete this sticky");
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert('Delete failed.');
            }
        });
    }

}